﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace CCF20Model
{
    public class CustomizeTypeCollection<T> : ObservableCollection<T>
    {
        private CCF20Entities ccf20Entities;
        private EnumCustomizedTable customizedTable;
        protected override void InsertItem(int index, T item)
        {
            switch (customizedTable)
            {
                case EnumCustomizedTable.MaintenanceType:
                    ccf20Entities.MaintenanceTypes.AddObject(item as MaintenanceType);
                    break;
                case EnumCustomizedTable.VehicleType:
                    ccf20Entities.VehicleTypes.AddObject(item as VehicleType);
                    break;
                
                case EnumCustomizedTable.SMSTemplate:
                    ccf20Entities.SMSTemplates.AddObject(item as SMSTemplate);
                    break;
                case EnumCustomizedTable.CustomizedTroubleCode:
                    ccf20Entities.CustomizedTroubleCodes.AddObject(item as CustomizedTroubleCode);
                    break;
                case EnumCustomizedTable.RemindList:
                    ccf20Entities.RemindLists.AddObject(item as RemindList);
                    break;
                
            }
            base.InsertItem(index, item);
            //ccf20Entities.SaveChanges();
        }

        protected override void RemoveItem(int index)
        {
            //ccf20Entities.MaintenanceTypes.DeleteObject(this[index]);
            switch (customizedTable)
            {
                case EnumCustomizedTable.MaintenanceType:
                    ccf20Entities.MaintenanceTypes.DeleteObject(this[index] as MaintenanceType);
                    break;
                case EnumCustomizedTable.VehicleType:
                    ccf20Entities.VehicleTypes.DeleteObject(this[index] as VehicleType);
                    break;
                
                case EnumCustomizedTable.SMSTemplate:
                    ccf20Entities.SMSTemplates.DeleteObject(this[index] as SMSTemplate);
                    break;
                case EnumCustomizedTable.CustomizedTroubleCode:
                    ccf20Entities.CustomizedTroubleCodes.DeleteObject(this[index] as CustomizedTroubleCode);
                    break;
                case EnumCustomizedTable.RemindList:
                    ccf20Entities.RemindLists.DeleteObject(this[index] as RemindList);
                    break;
            }
            //ccf20Entities.SaveChanges();
            base.RemoveItem(index);
        }
        public CustomizeTypeCollection(IQueryable<T> serviceRequest, CCF20Entities context)
            : base(serviceRequest)
        {
            Type t = typeof(T);
            customizedTable = MySharedHelp.ConvertTableNameToEnum(t.Name);
            ccf20Entities = context;
        }

    }

    public class CustomizeModel
    {
        CCF20Entities ccf20Ent;
        public CustomizeModel()
        {
            ccf20Ent = new CCF20Entities();
        }
        public CustomizeTypeCollection<VehicleType> GetVehicleType()
        {

            if (ZICMonitorModel.IsJava)
            {
                var query = from m in ccf20Ent.VehicleTypes
                            orderby m.VehicleTypeID descending
                            select m;
                return new CustomizeTypeCollection<VehicleType>(query, ccf20Ent);
            }
            else
            {
                var query = from m in ccf20Ent.VehicleTypes
                            where m.Manufacture.Equals("FAW-VW")
                            orderby m.VehicleTypeID descending
                            select m;
                return new CustomizeTypeCollection<VehicleType>(query, ccf20Ent);
            }

        }
        
        public CustomizeTypeCollection<CustomizedTroubleCode> GetCustomizedTroubleCode()
        {
            var query = from m in ccf20Ent.CustomizedTroubleCodes
                        orderby m.CustomizedTroubleCodeID descending
                        select m;
            return new CustomizeTypeCollection<CustomizedTroubleCode>(query, ccf20Ent);
        }
        
        public CustomizeTypeCollection<MaintenanceType> GetMaintenanceType()
        {
            if (ZICMonitorModel.IsJava)
            {
                var query = from m in ccf20Ent.MaintenanceTypes
                            orderby m.VehicleTypeID, m.Name, m.Priority
                            select m;
                return new CustomizeTypeCollection<MaintenanceType>(query, ccf20Ent);
            }
            else
            {
                var query = from m in ccf20Ent.MaintenanceTypes
                            where m.VehicleType.Manufacture.Equals("FAW-VW")
                            orderby m.VehicleTypeID, m.Name, m.Priority
                            select m;
                return new CustomizeTypeCollection<MaintenanceType>(query, ccf20Ent);
            }

             
        }
        public CustomizeTypeCollection<SMSTemplate> GetSMSTemplate()
        {
            var query = from m in ccf20Ent.SMSTemplates
                        orderby m.SMSTitle descending
                        select m;
            return new CustomizeTypeCollection<SMSTemplate>(query, ccf20Ent);
        }
        public CustomizeTypeCollection<SMSHistory> GetSMSHistory()
        {
            var query = from m in ccf20Ent.SMSHistories
                        orderby m.QueueTime descending
                        select m;
            return new CustomizeTypeCollection<SMSHistory>(query, ccf20Ent);
        }
        public CustomizeTypeCollection<RemindList> GetRemindList()
        {
            var query = from m in ccf20Ent.RemindLists
                        orderby m.RemindListID descending
                        select m;
            return new CustomizeTypeCollection<RemindList>(query, ccf20Ent);
        }
        public int GetMaxPriorityIDByMaintenanceType(int anchiID)
        {
            short? temp = ccf20Ent.MaintenanceTypes.Where(m => m.AnchiID == anchiID).Max(m => m.Priority);
            if (temp == null)
            {
                return 1;
            }
            else
            {
                return (int)temp;
            }
        }
        public void Update()
        {
            ccf20Ent.SaveChanges();
        }
    }
}
